var user_hot;
var user_pageNavObj = null;
var user_current=0;
$(".nav_user").on("click",function () {
    if(!user_hot){
        getUserAll(0);
    }
});
$(".user_close").on("click",function () {
    $("#accordion").empty();
});
$("#user_export").on("click",function () {
    // window.open("http://125.65.40.171/conference-check/exportExcelController/exportConventionerBaseInfo");
    window.open("exportExcelController/exportConventionerBaseInfo");
});
$("#user_select").on("click",function () {
    var phone=$("#user_phone").val();
    user_hot=undefined;
    $("#table_user").empty();
    getUserAll(0,phone);
});
function createUserTable(datas,num) {
    if(datas!=null){
        var data=[];
        $.each(datas,function (e,v) {
            var list={"name":v.name,"phoneNumber":v.phoneNumber,"password":v.password,"id":v.id};
            data.push(list);
        });
        if(num==0){
            var userReload=function(instance, td, row, col, prop, value, cellProperties){
                Handsontable.renderers.TextRenderer.apply(this, arguments);
                if(col==0){
                    td.innerHTML="";
                    var a=$("<a href='#' user_id='"+instance.getDataAtRowProp(row,"id")+"' class='user_detail' onclick='getUserDetail(this)' data-toggle='modal' data-target='#userDetailBody'>"+value+"</a>");
                    $(td).append(a);
                }else if(col==3){
                    td.innerHTML="";
                    var div=$("<div><button type='button' user_id='"+value+"' onclick='deleteUser(this)' class='btn btn-default user_operation'>删除</button></div>");
                    $(td).append(div);
                }
            };
            var container_user = document.getElementById('table_user');
            user_hot = new Handsontable(container_user, {
                data: data,
                autoColumnSize:true,
                rowHeaders: false,
                colHeaders: ["用户名","手机号码","密码","操作"],
                fillHandle:true,
                cells: function (row, col, prop){
                    var cellProperties = {};
                    if(row>=0){
                        cellProperties.renderer = userReload;
                    }
                    cellProperties.readOnly = true;
                    return cellProperties;
                }
            });
            $("#table_user").siblings("#hot-display-license-info").css("display","none");
        }else{
            user_hot.loadData(data);
            user_current=num;
        }
    }
}function deleteUser(t) {
    var id=$(t).attr("user_id");
    var params=JSON.stringify({"conventionerId":id});
    $.ajax({
        // url:'http://125.65.40.171/conference-check/conventionerController/deleteConventioner',
        url:'conventionerController/deleteConventioner',
        dataType:'json',
        data:{"param":params},
        type:"post",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success:function (data) {
            if(data.code==200){
                window.location.href="manage";
            }else if(data.code==500){
                alert("删除失败");
            }
        },
        error:function (XMLHttpRequest,textStatus,errorThrown) {
            alert('系统错误');
        }
    })
}
// 获取所有数据
function getUserAll(num,phone) {
    var par=0;
    if(num==0){
        par=1;
    }else{
        par=num;
    }
    if(phone==undefined){
        phone="";
    }
    var params=JSON.stringify({"pageNumber":par,"phoneNumber":phone});
    $.ajax({
        // url:'http://125.65.40.171/conference-check/conventionerController/selectConventionerBaseInfo',
        url:'conventionerController/selectConventionerBaseInfo',
        dataType:'json',
        data:{"param":params},
        type:"post",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success:function (data) {
            if(data.code==200){
                createUserTable(data.data.dataList,num);
                // 初始化分页
                if(user_current==num){
                    user_pageNavObj = new PageNavCreate("user_PageNavId",{
                        pageCount:data.data.pageTotal,//总页数
                        currentPage:par,//当前页
                        perPageNum:5//每页按钮数
                    });
                    user_pageNavObj.afterClick(userPageNavCallBack);
                }
            }else if(data.code==500){
                createUserTable(null,num);
                alert("当前没有数据");
            }
        },
        error:function (XMLHttpRequest,textStatus,errorThrown) {
            alert('系统错误');
        }
    })
}
function getUserDetail(t) {
    var id=$(t).attr("user_id");
    //问题：状态码参数可以不传递
    var params=JSON.stringify({"conventionerId":id,"isApproved":""});
    $.ajax({
        // url:'http://125.65.40.171/conference-check/conferenceController/selectConferenceInfoByConventionerId',
        url:'conferenceController/selectConferenceInfoByConventionerId',
        dataType:'json',
        data:{"param":params},
        type:"post",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success:function (data) {
            if(data.code==200){
                $.each(data.data,function (e,v) {
                    $("#accordion").append("<div class='panel panel-default'>" +
                        "<div class='panel-heading' role='tab' id='heading_"+v.conferenceId+"'>" +
                        "<h4 class='panel-title'>" +
                        "<a role='button' data-toggle='collapse' data-parent='#accordion' href='#collapse_"+v.conferenceId+"' aria-expanded='true' aria-controls='collapse_"+v.conferenceId+"'>" +
                        "<div class='row'>" +
                        "<div class='col-sm-6' style='float: left'>"+v.name+"</div>" +
                        "<div class='col-sm-6' style='float: right'>参会费用："+v.cost+"</div>" +
                        "</div>" +
                        "</a>" +
                        "</h4>" +
                        "</div>" +
                        "<div id='collapse_"+v.conferenceId+"' class='panel-collapse collapse in' role='tabpanel' aria-labelledby='heading_"+v.conferenceId+"'>" +
                        "<div class='panel-body'>" +
                        "<table>" +
                        "<thead>" +
                        "<tr>" +
                        "<th>会议时间</th>" +
                        "<th>会议地点</th>" +
                        "<th>签到状态</th>" +
                        "</tr>" +
                        "</thead>" +
                        "<tbody></tbody>" +
                        "</table>" +
                        "</div>" +
                        "</div>" +
                        "</div>");
                    $.each(v.addressList,function (e,v) {
                        var status="";
                        if(v.status==0){
                            status="未签到";
                        }else if(v.status==1){
                            status="已签到";
                        }
                        $("#accordion").find(".panel:last").find(".panel-body").find("tbody").append("<tr>" +
                            "<td>"+new Date(v.startDate).Format('yyyy/MM/dd HH:mm')+"-"+new Date(v.endDate).Format('yyyy/MM/dd HH:mm')+"</td>" +
                            "<td>"+v.site+"</td>" +
                            "<td>"+status+"</td>" +
                            "</tr>");
                    });
                });
            }else if(data.code==500){
                alert("当前没有数据");
            }
        },
        error:function (XMLHttpRequest,textStatus,errorThrown) {
            alert('系统错误');
        }
    })
}
//翻页按钮点击后触发的回调函数
function userPageNavCallBack(clickPage){
    // console.log(clickPage);
    // 更新分页插件
    // pageNavCreate("PageNav",pageCount,clickPage,pageNavCallBack);
    user_pageNavObj = new PageNavCreate("user_PageNavId",{
        pageCount:getPageSet().pageCount,//总页数
        currentPage:clickPage,//当前页
        perPageNum:getPageSet().perPageNum,//每页按钮数
    });
    user_pageNavObj.afterClick(userPageNavCallBack);
    if(user_current!=clickPage){
        getUserAll(clickPage);
    }
}
